package com.erick.contacts.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.erick.R;
import com.erick.contacts.adapter.ContactsBaseAdapter;
import com.erick.contacts.bean.ContactsBean;
import com.erick.contacts.helper.DBHelper;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * @Title: FriendsListActivity.java
 * @Package com.erick.contacts
 * @Description: 通讯录列表
 * @company: 科贸学院
 * @author ChenBin
 * @date 2012-9-21 上午9:20:22
 * @version V1.0
 */
public class ContactsListActivity extends Activity implements
		OnItemClickListener {

	private ListView contactsListView;
	private ContactsBaseAdapter adapter;
	private List<Map<String, Object>> list = null;
	private Map<String, Object> map;
	private DBHelper helper;
	private Cursor c;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.contacts_listview);

		contactsListView = (ListView) findViewById(R.id.contactsList);
		helper = new DBHelper(this);
		initData();
		contactsListView.setOnItemClickListener(this);
	}

	private void initData() {
		helper = new DBHelper(this);
		SQLiteDatabase db = helper.getReadableDatabase();
		c = db.query("contact_list", new String[] { "name", "phone", "email" },
				null, null, null, null, null);
		list = new ArrayList<Map<String, Object>>();
		while (c.moveToNext()) {
			ContactsBean bean = new ContactsBean();
			map = new HashMap<String, Object>();
			map.put("name",
					bean.setContactName(c.getString(c.getColumnIndex("name"))));
			map.put("phone", bean.setContactPhone(c.getString(c
					.getColumnIndex("phone"))));
			map.put("email", bean.setContactPhone(c.getString(c
					.getColumnIndex("email"))));
			list.add(map);
		}
		adapter = new ContactsBaseAdapter(this, list);
		contactsListView.setAdapter(adapter);
		adapter.notifyDataSetChanged();// 刷新数据
	}

	public void onBtnListener(View v) {
		int viewId = v.getId();
		switch (viewId) {
		case R.id.add:
			ContactsListActivity.this.startActivity(new Intent(
					ContactsListActivity.this, ContactsAddActivity.class));
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Map<String, Object> m = list.get(position);
		Intent intent = new Intent();
		intent.putExtra("name", String.valueOf(m.get("name")));
		intent.putExtra("phone", String.valueOf(m.get("phone")));
		intent.putExtra("email", String.valueOf(m.get("email")));
		intent.setClass(ContactsListActivity.this, ContactsDetailActivity.class);
		startActivity(intent);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			System.exit(0);
		}
		return true;
	}

}
